import HomeIndex from '@/views/Home/HomeIndex.vue'
import LoginIndex from '@/views/Login/LoginIndex.vue'
import { createRouter, createWebHistory } from 'vue-router'
import { useAdminStore } from '@/stores/AdminStore'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '',
      name: 'login',
      component: LoginIndex
    },
    {
      path: '/',
      name: 'login',
      component: LoginIndex
    },
    {
      path: '/home',
      name: 'home',
      component: HomeIndex
    },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue')
    // }
  ]
})

//  路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = useAdminStore().getTokenInfo() ? true : false
  if(to.path == '/' || to.path == ''){
      next()
  }else {
      isLogin ? next() : next('/')
  }
})

export default router
